package com.av.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;

public class BaseActivity extends Activity {
	public final static String C2DM_REGISTRATION_ID = "RegistrationId";
	final private Object dialogLock = new Object();
	final private Object loadingDialogLock = new Object();
	private boolean isShowing = false;
	private ProgressDialog loadingDialog;
	private AlertDialog dialog;
	Dialog myDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		myDialog = new Dialog(this,R.style.CustomDialogTheme);
	}
	
	@Override  
    protected void onPause() {
			isShowing=false;
			if (loadingDialog != null) {
				loadingDialog.dismiss();
				loadingDialog = null;
			}
		
			if (dialog != null) {
				dialog.cancel();
				dialog = null;
			}
		
    	super.onPause();  
    }
	
    @Override
    protected void onPostResume(){
    	
    	isShowing=true;
    	super.onPostResume();
    }
	
	public synchronized void alertAndFinish(final String message,final String title) {

		runOnUiThread(new Runnable() {
			public void run() {
				alertWithOnClickHandler(message,title, new Handler() {
					@Override
					public void handleMessage(Message msg) {
						finish();
					}
				});
			}
		});

	}
	

	public synchronized void alertWithOnClickHandler(final String message,final String title,
			final Handler... handlers) {
		
		
		synchronized (dialogLock) {
			runOnUiThread(new Runnable() {
				public void run() {
					//if (dialog == null) {
						dialog = new AlertDialog.Builder(BaseActivity.this)
								.setNegativeButton("Tamam",
										new DialogInterface.OnClickListener() {

											public void onClick(
													DialogInterface dialog,
													int which) {
												dialog.dismiss();

												Message msg = Message.obtain();
												msg.what = 1;
												for (Handler handler : handlers)
													handler.sendMessage(msg);
											}
										}).setCancelable(false).create();
					//}
					dialog.setOwnerActivity(BaseActivity.this);
					dialog.setMessage(message);
					dialog.setTitle(title);
					if(isShowing) dialog.show();
				}
			});
		}
		
		
	}
	
	
	public synchronized void confirmAlertWithOnClickHandler(final String title,final String neg,final String pos,  final String message,
			final Handler... handlers) {

		synchronized (dialogLock) {
			runOnUiThread(new Runnable() {
				public void run() {
					//if (dialog == null) {
						dialog = new AlertDialog.Builder(BaseActivity.this)
								.setTitle(title)
								.setNegativeButton(neg,
										new DialogInterface.OnClickListener() {

											public void onClick(
													DialogInterface dialog,
													int which) {
												dialog.dismiss();
											}
										})
								.setPositiveButton(pos,
										new DialogInterface.OnClickListener() {

											public void onClick(
													DialogInterface dialog,
													int which) {
												dialog.dismiss();

												Message msg = Message.obtain();
												msg.what = 1;
												for (Handler handler : handlers)
													handler.sendMessage(msg);
											}
										}).create();
					//}
					dialog.setOwnerActivity(BaseActivity.this);
					dialog.setMessage(message);
					if(isShowing) dialog.show();
				}
			});
		}
	
	}
    public synchronized void alert(int res){
    	alert(getString(res));
    }
	
	public synchronized void alert(final String message){
		
		synchronized (dialogLock) {
    	runOnUiThread(new Runnable() {
			public void run() {
				//if(dialog == null){
		    		dialog = new AlertDialog.Builder(BaseActivity.this).setNegativeButton("Tamam", new DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					}).create();
		    	//}
				dialog.setOwnerActivity(BaseActivity.this);
				dialog.setMessage(message);
			
				if(isShowing) dialog.show();
			}
		});
		}
    }
	
	
    public synchronized void showLoading(final String message){
    	synchronized (loadingDialogLock) {
    	runOnUiThread(new Runnable() {
			public void run() {
				if (loadingDialog == null) {
					loadingDialog = new ProgressDialog(BaseActivity.this);
					loadingDialog.setCancelable(false);
					loadingDialog.setCanceledOnTouchOutside(false);
					loadingDialog.setMessage("Loading...");
					WindowManager.LayoutParams lp = loadingDialog.getWindow()
							.getAttributes();
					lp.gravity = Gravity.CENTER;
					loadingDialog.setOwnerActivity(BaseActivity.this);
				}
				
				try { if(isShowing) loadingDialog.show(); } 
				catch(Exception e){Log.e("avealig", "Error in showDialog", e);}
			}
		});
    	}
    }
    
    public synchronized void hideLoading(){
    	synchronized (loadingDialogLock) {
    		if(loadingDialog == null) return;
    		try{
    			loadingDialog.dismiss();
    		}
    		catch (Exception e) {
				Log.w("gncplay", "hideLoading exception", e);
			}    		
		}
    }
	

    

}
